﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.MessageBox;

using Global.Class.Library;
using DataAccessLayer;

namespace PresentationLayer
{
    static class Startup
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            bool isLoginOK = false;



            ////KHONG LOAD DATABASE
            //GlobalVariables.LocationID = 1;
            //isLoginOK = true;



            try
            {

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                GlobalRegistry.ShowError = true;
                GlobalMsADO.ServerName = GlobalRegistry.Read("ServerName"); //"7HOME-PC\\SQLEXPRESS";// 
                GlobalMsADO.DatabaseName = GlobalRegistry.Read("DatabaseName"); // "ERmgrUIP";// 

                if (Screen.PrimaryScreen.Bounds.Size.Width < 1280) throw new Exception("This program requires a display resolution equal or greater than 1280x1024." + "\r\n\n" + "Please check your display settings and try again."); //Screen.PrimaryScreen.Bounds.Size.Height < 1024 || 


                //GlobalMsADO.ServerName = "HOME-PC\\SQLEXPRESS";// 
                //GlobalMsADO.DatabaseName = "ERmgrUIP22";//

                //GlobalMsADO.ServerName = "192.168.10.22";// 
                //GlobalMsADO.DatabaseName = "ERmgrUIP22";//


                while (!isLoginOK && GlobalVariables.LocationID >= -1)//Try to open startup database, Exit if user cancel to input ServerName and DatabaseName
                {

                    if (GlobalMsADO.ServerName == null || GlobalMsADO.DatabaseName == null || GlobalMsADO.ServerName == "" || GlobalMsADO.DatabaseName == "")//Show Get Server Name Dialog
                    {
                        PublicGetServerName publicGetServerName = new PublicGetServerName();
                        if (publicGetServerName.ShowDialog() != DialogResult.OK) { GlobalVariables.LocationID = -2; }
                        if (publicGetServerName.DialogResult == DialogResult.OK)

                            publicGetServerName.Dispose();
                    }

                    if (GlobalVariables.LocationID >= -1)//Do not execute if user cancel to input ServerName and DatabaseName
                    {
                        try
                        {
                            if (GlobalMsADO.MainDataAccessConnection(true).State == ConnectionState.Open)//Try to open new connection
                            {
                                isLoginOK = true;
                                GlobalVariables.LocationID = 1;
                            }
                            else
                            {
                                GlobalMsADO.ServerName = "";
                                GlobalMsADO.DatabaseName = "";
                            }
                        }
                        catch
                        {
                            GlobalMsADO.ServerName = "";
                            GlobalMsADO.DatabaseName = "";
                        }

                    }
                }


                if (GlobalVariables.LocationID > 0)
                {
                    #region CHECK VERSION

                    SQLDatabase.ExecuteNonQuery("UPDATE PublicVersionMaster SET VersionID = " + GlobalVariables.VersionID + " WHERE VersionID < " + GlobalVariables.VersionID);
                    DataTable dataTable = SQLDatabase.GetDataTable("SELECT MAX(VersionID) AS VersionID FROM PublicVersionMaster");

                    if (dataTable.Rows.Count <= 0)
                        throw new System.ArgumentException("Can not validate the version of this program.");
                    else
                        if (GlobalVariables.VersionID != (int)dataTable.Rows[0]["VersionID"])
                            throw new System.ArgumentException("The program ERmgr on this computer is not the lasted version." + "\r\n" + "Please contact your admin for more information. Thank you!");
                    
                    #endregion CHECK VERSION




                    PublicApplicationLogon publicApplicationLogon = new PublicApplicationLogon();

                    if (publicApplicationLogon.ShowDialog() == DialogResult.OK)
                    {
                        CommonStoreProcedure commonStoreProcedure = new CommonStoreProcedure();//Store SQL Procedure

                        CommonDatabaseMethod commonDatabaseMethod = new CommonDatabaseMethod();
                        CommonDatabaseView commonDatabaseView = new CommonDatabaseView();

                        Application.Run(new frmMDIMain());
                        //Application.Run(new TestAutoComplete());
                    }
                    publicApplicationLogon.Dispose();


                }
            }

            catch (Exception ex)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(new Form(), ex);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }

        }


    }
}
